﻿using Common;
using Common.DataModels;
using PLC.Domain.Models;

namespace PLC.Domain.Entity;
/// <summary>
/// 生产的结果数据(适用于生产完成  给信号让我来采集一次的数据)
/// </summary>
public class ProductionData:AggregateRoot
{
    /// <summary>
    /// 任务编号
    /// </summary>
    public string TaskCode { get; set; }
    /// <summary>
    /// 上端板编号
    /// </summary>
    public string UpperEndPlateCode { get;  set; }
    /// <summary>
    /// 下端板编号
    /// </summary>
    public string LowerEndPlateCode { get;  set; }
    /// <summary>
    /// 设备id
    /// </summary>
    public long DeviceId { get;  set; }
    
    /// <summary>
    /// 结果值(0 没有结果  1 ok 2 ng)
    /// </summary>
    public int Result { get;  set; }
    /// <summary>
    /// 设备类型
    /// </summary>
    public int DeviceType { get; set; }
    /// <summary>
    /// json格式的数据
    /// </summary>
    public string Data { get; set; }
    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreateTime { get;  set; }
    /// <summary>
    /// 是否删除
    /// </summary>
    public bool IsDelete { get;  set; }

    public ProductionData() { }
    
    public  ProductionData  Build()
    {
        base.Id = SnowflakeHelper.GetId();
        this.IsDelete = false;
        this.CreateTime = DateTime.Now;
        return this;
    }

    public ProductionData SetData(IEnumerable<PLCDataModel> data)
    {
        if (data==null||!data.Any())
        {
            this.Data = string.Empty;
        }

        this.Data = JsonHelper.SerializeObject(data);
        return this;
    }
    public ProductionData SetTaskCode(string? taskCode)
    {
        this.TaskCode = string.IsNullOrWhiteSpace(taskCode)?string.Empty:taskCode;
        return this;
    }

    public ProductionData SetUpperEndPlateCode(string? upperEndPlateCode)
    {
        this.UpperEndPlateCode =string.IsNullOrWhiteSpace(upperEndPlateCode)?string.Empty:upperEndPlateCode ;
        return this;
    }

    public ProductionData SetLowerEndPlateCode(string? lowerEndPlateCode)
    {
        this.LowerEndPlateCode = string.IsNullOrWhiteSpace(lowerEndPlateCode) ? string.Empty : UpperEndPlateCode;
        return this;
    }

    public ProductionData SetDeviceId(long deviceId)
    {
        this.DeviceId = deviceId;
        return this;
    }


    public ProductionData SetResult(int result)
    {
        this.Result = result;
        return this;
    }

}